package system;

import datacontrol.DAO;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.Toolkit;

/**
 *haal alle voltooide enquete's op en laat hiervan de statistieken zien.
 * 
 * @author Joeri
 */
public class LaadStatistieken extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (!Toolkit.checkLogin(request)) {
            // Ogenschijnlijk is de gebruiker niet ingelogd
            response.sendRedirect("EnqueteOphaler");
            return;
        }
        ArrayList<ArrayList<HashMap<String, String>>> result = new ArrayList<ArrayList<HashMap<String, String>>>();
        
        ArrayList<HashMap<String, String>> vragenId = new ArrayList<HashMap<String, String>>();
        int id;
        id = Integer.parseInt(request.getParameter("id"));
        try {
            vragenId = DAO.selectQuery("SELECT `Vraag_idVraag` FROM `VraagInEnquete` WHERE `Enquete_idEnquete` = " + id);
            System.out.println("vragenId: " + vragenId.toString());
            //result = DAO.selectQuery("SELECT type, vraagstelling,antwoord, count (antwoord ) AS aantal,(SELECT count(Vraag_idVraag) FROM Antwoord WHERE Vraag_idVraag ='" + id + "') AS iets FROM Antwoord,Vraag WHERE Vraag_idVraag ='" + id + "' AND Antwoord.Vraag_idVraag = Vraag.rowid AND `Vraag`.`type` != 'Open'GROUP BY antwoord");
            for (int i = 0; i < vragenId.size(); i++) {
                result.add(DAO.selectQuery("SELECT type, vraagstelling, antwoord, COUNT(antwoord ) AS aantal,"
                        + "(SELECT COUNT(Vraag_idVraag) "
                        + "FROM Antwoord "
                        + "WHERE Vraag_idVraag ='" + vragenId.get(i).get("Vraag_idVraag") + "') AS iets "
                        + " FROM Antwoord, Vraag "
                        + "WHERE Vraag_idVraag ='" + vragenId.get(i).get("Vraag_idVraag") + "' "
                        + " AND Antwoord.Vraag_idVraag = Vraag.rowid "
                        + " AND `Vraag`.`type` != 'Open' "
                        + "GROUP BY antwoord"));
            }
        } catch (SQLException ex) {
            
        }
        request.setAttribute("result", result);
        for (int i = 0; i < result.size(); i++) {
            for (int j = 0; j < result.get(i).size(); j++) {
                result.get(i).get(j).put("percentage", String.valueOf((Double.parseDouble(result.get(i).get(j).get("aantal").toString()) / Double.parseDouble(result.get(i).get(j).get("iets").toString())) * 100.0) + "%");
            }
        }
        System.out.println(result.toString());
        getServletContext().getRequestDispatcher("/statistieken.jsp").forward(request, response);
        return;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
